//
//  CGestureDataOperation.m
//  Invest
//
//  Created by 邓晓羽 on 15/1/9.
//  Copyright (c) 2015年 chenliang. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CGestureDataOperation.h"

@implementation CGestureDataOperation

+(void)setPasswordByUser:(NSString *)strUser password:(NSString *)strPassword
{
    if( strUser==nil || [strUser isEqualToString:@""] || strPassword==nil )
        return;
    
    //申请空间，保存用户手势数据
    [self initGestureDatas];
    
    NSMutableDictionary* dicDatas = [NSMutableDictionary dictionaryWithDictionary:[UserDefaultsUtil objectForKey:GLOBALDATA_GESTURES]];
    NSMutableDictionary *dicUser = [NSMutableDictionary dictionaryWithDictionary:[dicDatas objectForKey:strUser]];
    if( dicUser==nil || dicUser.count==0 )
    {
        dicUser = [self initUserGestureSets:strUser];
    }
    [dicUser setValue:strPassword forKey:GESTURE_PASSWORD];
    [dicDatas setValue:dicUser forKey:strUser];
    [UserDefaultsUtil setObject:dicDatas forKey:GLOBALDATA_GESTURES];
}

+(NSString*)getPasswordByUser:(NSString *)strUser
{
    if( strUser==nil || [strUser isEqualToString:@""] )
        return @"";
    
    //申请空间，保存用户手势数据
    [self initGestureDatas];
    
    NSMutableDictionary* dicDatas = [NSMutableDictionary dictionaryWithDictionary:[UserDefaultsUtil objectForKey:GLOBALDATA_GESTURES]];
    NSMutableDictionary* dicUser = [NSMutableDictionary dictionaryWithDictionary:[dicDatas objectForKey:strUser]];
    if( dicUser==nil || dicUser.count==0 )      //若是新用户，则生成默认数据
    {
        dicUser = [self initUserGestureSets:strUser];
        [dicDatas setValue:dicUser forKey:strUser];
        [UserDefaultsUtil setObject:dicDatas forKey:GLOBALDATA_GESTURES];
    }
    NSString* password = [dicUser objectForKey:GESTURE_PASSWORD];
    return password;
}

+(void)setGestureFlagByUser:(NSString *)strUser on:(BOOL)bFlag
{
    if( strUser==nil || [strUser isEqualToString:@""] )
        return;
    
    //申请空间，保存用户手势数据
    [self initGestureDatas];

    NSMutableDictionary* dicDatas = [NSMutableDictionary dictionaryWithDictionary:[UserDefaultsUtil objectForKey:GLOBALDATA_GESTURES]];
    NSMutableDictionary *dicUser = [NSMutableDictionary dictionaryWithDictionary:[dicDatas objectForKey:strUser]];
    
    if( dicUser==nil || dicUser.count==0 )
    {
        dicUser = [self initUserGestureSets:strUser];
    }
    [dicUser setValue:(bFlag==YES?@"1":@"0") forKey:GESTURE_ON];
    [dicDatas setValue:dicUser forKey:strUser];
    [UserDefaultsUtil setObject:dicDatas forKey:GLOBALDATA_GESTURES];
}

+(BOOL)getGestureFlagByUser:(NSString *)strUser
{
    if( strUser==nil || [strUser isEqualToString:@""])
        return NO;
    
    //申请空间，保存用户手势数据
    [self initGestureDatas];
    
    NSMutableDictionary* dicDatas = [NSMutableDictionary dictionaryWithDictionary:[UserDefaultsUtil objectForKey:GLOBALDATA_GESTURES]];
    NSMutableDictionary* dicUser = [NSMutableDictionary dictionaryWithDictionary:[dicDatas objectForKey:strUser]];
    NSString* gestureOn = @"0";
    if( dicUser==nil || dicUser.count==0 )
    {
        dicUser = [self initUserGestureSets:strUser];
        [dicDatas setValue:dicUser forKey:strUser];
        [UserDefaultsUtil setObject:dicDatas forKey:GLOBALDATA_GESTURES];
    }
    gestureOn = [dicUser objectForKey:GESTURE_ON];
    return [gestureOn isEqualToString:@"1"];
}

+(void)setFirstSetByUser:(NSString *)strUser on:(BOOL)bFlag
{
    if( strUser==nil || [strUser isEqualToString:@""] )
        return;
    
    //申请空间，保存用户手势数据
    [self initGestureDatas];
    
    NSMutableDictionary* dicDatas = [NSMutableDictionary dictionaryWithDictionary:[UserDefaultsUtil objectForKey:GLOBALDATA_GESTURES]];
    NSMutableDictionary *dicUser = [NSMutableDictionary dictionaryWithDictionary:[dicDatas objectForKey:strUser]];
    
    if( dicUser==nil || dicUser.count==0 )
    {
        dicUser = [self initUserGestureSets:strUser];
    }
    [dicUser setValue:(bFlag==YES?@"1":@"0") forKey:GESTURE_SETONLOGIN];
    [dicDatas setValue:dicUser forKey:strUser];
    [UserDefaultsUtil setObject:dicDatas forKey:GLOBALDATA_GESTURES];
}

+(BOOL)getFirstSetByUser:(NSString *)strUser
{
    if( strUser==nil || [strUser isEqualToString:@""])
        return NO;
    
    //申请空间，保存用户手势数据
    [self initGestureDatas];
    
    NSMutableDictionary* dicDatas = [NSMutableDictionary dictionaryWithDictionary:[UserDefaultsUtil objectForKey:GLOBALDATA_GESTURES]];
    NSMutableDictionary* dicUser = [NSMutableDictionary dictionaryWithDictionary:[dicDatas objectForKey:strUser]];
    NSString* strFirstSet = @"0";
    if( dicUser==nil || dicUser.count==0 )
    {
        dicUser = [self initUserGestureSets:strUser];
        [dicDatas setValue:dicUser forKey:strUser];
        [UserDefaultsUtil setObject:dicDatas forKey:GLOBALDATA_GESTURES];
    }
    strFirstSet = [dicUser objectForKey:GESTURE_SETONLOGIN];
    return [strFirstSet isEqualToString:@"1"];
}

+(void)initGestureDatas
{
    NSMutableDictionary* dicDatas = [UserDefaultsUtil objectForKey:GLOBALDATA_GESTURES];
    if( dicDatas==nil )
    {
        dicDatas = [[NSMutableDictionary alloc] init];
        [UserDefaultsUtil setObject:dicDatas forKey:GLOBALDATA_GESTURES];
    }
}

+(NSMutableDictionary*)initUserGestureSets:(NSString*)strUser
{
    NSMutableDictionary* dicUser = [[NSMutableDictionary alloc] init];
    [dicUser setValue:strUser forKey:@"userName"];
    [dicUser setValue:@"" forKey:GESTURE_PASSWORD];
    [dicUser setValue:@"1" forKey:GESTURE_ON];
    [dicUser setValue:@"1" forKey:GESTURE_SETONLOGIN];
    
    return dicUser;
}

@end
